//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
import delimited using "data\GA_precincts.csv",clear

keep if raceid == 1
drop race
replace candidates = substr(candidates,-4,1)
drop candids registration raceid
replace type = substr(type,1,3)
compress

reshape wide votes,i(county precinct time type) j(candidates) string
reshape wide votes*,i(county precinct time)j(type) string
gen keep_time = clock(time,"YMD hms") > clock("2020-11-03-20:00:00","YMD hms")
keep if keep_time == 1
drop if precinct == "-1"

gen time_num = clock(time,"YMD hms") 


** timing of release

preserve
gen hours_since_closing = 0
collapse (max) hours_since_closing,by(county precinct)
save "data\scratch\hours_since_closing0", replace
restore

preserve
gen hours_since_closing = 60
collapse (max) hours_since_closing,by(county precinct)
save "data\scratch\hours_since_closing60", replace
restore



gen hours_since_closing = time_num - clock("2020-11-03 20:00:00","YMD hms")
replace hours_since_closing = hours_since_closing / (60*60*1000) - 1

egen votesabs = rowtotal(votes?abs)
egen votesadv = rowtotal(votes?adv)
egen votesele = rowtotal(votes?ele)

egen maxvotesabs = max(votesabs), by(county precinct)
egen maxvotesadv = max(votesadv), by(county precinct)
egen maxvotesele = max(votesele), by(county precinct)

gen votesabspct = 100*votesabs/maxvotesabs
gen votesadvpct = 100*votesadv/maxvotesadv
gen voteselepct = 100*votesele/maxvotesele

append using "data\scratch\hours_since_closing0"
append using "data\scratch\hours_since_closing60"
sort county precinct hours_since_closing

replace votesabspct = 0 if hours_since_closing == 0
replace votesadvpct = 0 if hours_since_closing == 0
replace voteselepct = 0 if hours_since_closing == 0

replace votesabspct = votesabspct[_n-1] if hours_since_closing == 60
replace votesadvpct = votesadvpct[_n-1] if hours_since_closing == 60
replace voteselepct = voteselepct[_n-1] if hours_since_closing == 60

keep if hours_since_closing <= 60

replace hours_since_closing = hours_since_closing + 1

sort county precinct hours_since_closing

twoway (line votesabspct hours_since_closing if hours_since < 60.001, lcolor(%90) lwidth(vvvthin) connect(stairstep) cmissing(n)) ///
       (line votesabspct hours_since_closing if hours_since < 60.001 & precinct == "East Macon 1", lcolor(red) lwidth(thin) connect(stairstep) cmissing(n)) if county ~= "Fulton" ///
       , ///
	   ytitle(" ") ///
       xtitle(Hours since closing) ///
	   legend(off) ///
	   title(Absentee-by-mail votes) ///
	   ylabels(0 20 40 60 80 100 "100%")
graph export "figures\fig19.png", replace width(2500)


twoway (line votesadvpct hours_since_closing if hours_since < 60.001, lcolor(%90) lwidth(vvvthin) connect(stairstep) cmissing(n)) ///
        (line votesadvpct hours_since_closing if hours_since < 60.001 & precinct == "East Macon 1", lcolor(red) lwidth(thin) connect(stairstep) cmissing(n)) ///
      , ///
	   ytitle(" ") ///
       xtitle(Hours since closing) ///
	   legend(off) ///
	   title(Early-in-person votes) ///
	   ylabels(0 20 40 60 80 100 "100%")
graph export "figures\fig20.png", replace width(2500)
	   

twoway (line voteselepct hours_since_closing if hours_since < 60.001, lcolor(%90) lwidth(vvvthin) connect(stairstep) cmissing(n)) ///
        (line voteselepct hours_since_closing if hours_since < 60.001 & precinct == "East Macon 1", lcolor(red) lwidth(thin) connect(stairstep) cmissing(n)) ///
      , ///
	   ytitle(" ") ///
       xtitle(Hours since closing) ///
	   legend(off) ///
	   title(Election Day votes) ///	  
   	   ylabels(0 20 40 60 80 100 "100%")
graph export "figures\fig21.png", replace width(2500)
